package com.it.myd.controller.main;

import com.it.myd.bean.box.Article;
import com.it.myd.bean.main.File;
import com.it.myd.bean.main.ReturnValue;
import com.it.myd.bean.main.User;
import com.it.myd.module.main.gameInfo.bean.Game;
import com.it.myd.module.main.gameInfo.service.GameInfoService;
import com.it.myd.service.main.LoginService;
import com.it.myd.service.main.module.article.ArticleService;
import com.it.myd.service.main.user.UserChangerService;
import com.it.myd.service.mainService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {


    @Autowired
    private LoginService loginService;
    @Autowired
    private UserChangerService userCService;
    @Autowired
    private ArticleService artService;
    @Autowired
    private GameInfoService gameInfoService;

    @RequestMapping("gologin")
    public String gologin(Model model,HttpServletRequest request) {
        model.addAttribute("user",new User());
        model.addAttribute("a", "123");
        return "main/login";
    }

    @RequestMapping("gomain")
    public String login(Model model,@ModelAttribute User user_t, Map<String, Object> map, HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = loginService.CheckUser(user_t);
        ReturnValue<String> ret = new ReturnValue<>();
        if(user != null){
            File userimg = loginService.getUserProFile(user);
            user_t.setUser_sex(user.getUser_sex());
            user_t.setUser_grjj(user.getUser_grjj());
            user_t.setUser_id(user.getUser_id());
            user_t.setUser_img(userimg);

            //更新用户状态
            user_t.setUser_status("20003");
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            user_t.setUser_status_time(sdf.format(System.currentTimeMillis()));
            userCService.updateUserStatus(user_t);

            session.setAttribute("user",user_t);
            model.addAttribute("onflag",0);
            Article article = new Article();
            article.setUser(user);
            List<Article> articleList = artService.selectArticlesHot(article);
            model.addAttribute("articleList",articleList);
            List<Game> gameList = gameInfoService.getGameInfo();
            model.addAttribute("gameList",gameList);
            return "main/main";
        }
        ret.setRetCode("9999");
        ret.setRetDesc("登录失败,请确定账号密码");

        model.addAttribute("user",new User());
        model.addAttribute("a", "123");
        model.addAttribute("ret",ret);
        return "main/login";
    }

}
